<?php
namespace app\first\controller;
use think\facade\Cache;
use think\facade\Config;
/**
 * Class OCR
 * @title 图片识别
 * @url /v1/ocr
 * @desc  有关于用户的接口
 * @route('v1/ocr')
 * @version 1.0
 * @readme 阿里云接口
 * @package app\first\controller
 */
class Ocr extends Base{
    //是否开启授权认证
    public $apiAuth = false;
    //附加方法
    protected $extraActionList = [];
    protected $client = null;
    protected $token = null;

    /**
     * Ocr constructor.
     * @throws \Expnull\Ocr\Exceptions\HttpException
     */
    public function __construct(){
        parent::__construct();
        $this->client = new \Expnull\Ocr\Ocr(Config::get('baidu_ocr.apiKey'),Config::get('baidu_ocr.secretKey'));
        $name = Config::get('baidu_ocr.apiKey').'_token';
        if(!Cache::has($name)){
           $token = $this->client->getToken();
           Cache::set($name,$token,60 * 60 * 24 * 29);
           $this->token = $token;
        }else{
            $this->token = Cache::get($name);
        }
        $this->client->setProbability('true');
        $this->client->setDetectLanguage('true');
    }

    /**
     * @param $file
     * @throws \Expnull\Ocr\Exceptions\HttpException
     */
    public function index(){
    }

    /**
     * @title 身份证识别
     * @method get
     * @param string $file 文件地址/base64文件 true
     * @param int $type 识别类型 true 1正面,2反面 1
     * @return Object  用户信息
     * @route('v1/ocr/id_card','post')
     * @throws \Expnull\Ocr\Exceptions\HttpException
     */
    public function id_card($file='',$type = 1){
        if(empty($file)){
            return $this->sendError(0,'缺少参数');
        }
        if(strpos($file,'http://')!==false){
            $this->client->setImageUrl($file);
        }else{
            $this->client->setImage($file);
        }
        $words = $this->client->getImgWords($this->token['access_token']);
        $_data = [];
        if($type==1){
            foreach ($words['words_result'] as $k => $v){
                if(strpos($v['words'],'名')!==false){
                    $tmp = explode('名',$v['words']);
                    $_data['name'] = end($tmp);
                }elseif(strpos($v['words'],'性别')!==false){
                    $tmp = explode('性别',$v['words']);
                    $tmp1 = explode('民族',end($tmp));
                    $_data['sex'] = $tmp1[0];
                    $_data['minzu'] = $tmp1[1];
                }elseif(strpos($v['words'],'身份号码')!==false){
                    $tmp = explode('身份号码',$v['words']);
                    $_data['no'] = end($tmp);
                }elseif(strpos($v['words'],'出生')!==false){
                    $tmp = explode('出生',$v['words']);
                    $_data['birthday'] = end($tmp);
                }else{
                    $_data['address'][] = ' '.$v['words'];
                }
            }
            $_data['address'] = str_replace('住址','',
                str_replace(' ','',
                    implode(' ',$_data['address'])));
        }else{
            foreach ($words['words_result'] as $k => $v){
                if(strpos($v['words'],'签发机关')!==false){
                    $tmp = explode('签发机关',$v['words']);
                    $_data['authority'] = end($tmp);
                }elseif(strpos($v['words'],'有效期限')!==false){
                    $tmp = explode('有效期限',$v['words']);
                    $_data['expiry_date'] = end($tmp);
                }else{
                    $_data['sign'][] = ' '.$v['words'];
                }
            }
            $_data['sign'] = str_replace('た。','',
                str_replace(' ','',
                    implode(' ',$_data['sign'])));
        }
        return $this->sendSuccess($_data);
    }
}